package mc747.servicos.autenticacao;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;

public class Builder {

	public static Retorno login(String password, String email){
		HttpClient client = new HttpClient();
		String[] response;
		Retorno r = new Retorno();
      
		String params = "r=autenticacao/login&password=" + password + "&email=" + email;
		GetMethod method = new GetMethod("http://server.felipegasparini.com/autenticacao/index.php" + "?" + params);
      
		try {
			System.err.println("Method: " + method.getURI());
		} catch (URIException e1) {
			e1.printStackTrace();
		}
      
		try{
			client.executeMethod(method);
//			System.err.println("response: " + method.getResponseBodyAsString());
			response = method.getResponseBodyAsString().split("\"");
			if(response[1].equals("erro")){
				r.setErro(response[3]);
//				System.err.println("erro: " + r.getErro());
			}else{
				r.setCpf(response[3]);
				r.setEmail(response[7]);
//				System.err.println("cpf: " + r.getCpf());
//				System.err.println("email: " + r.getEmail());
			}
//			for(int i = 0; i < response.length; i++){
//				System.err.println("string: " + response[i]);
//			}

		}catch(Exception e) { 
			System.err.println(e); 
		}
		method.releaseConnection();
		return r;
	}
 
	public static Retorno cadastro(String password, String email, String cpf){
		HttpClient client = new HttpClient();
		String[] response;
		Retorno r = new Retorno();
      
		String params = "r=autenticacao/cadastro&password=" + password + "&email=" + email + "&cpf=" + cpf;
		GetMethod method = new GetMethod("http://server.felipegasparini.com/autenticacao/index.php" + "?" + params);
      
		try {
			System.err.println("Method: " + method.getURI());
		} catch (URIException e1) {
			e1.printStackTrace();
		}
      
		try{
			client.executeMethod(method);
			System.err.println("response: " + method.getResponseBodyAsString());
			response = method.getResponseBodyAsString().split("\"");
			if(response[1].equals("erro")){
				r.setErro(response[3]);
				System.err.println("erro: " + r.getErro());
			}else{
				r.setCpf(response[3]);
				r.setEmail(response[7]);
//				System.err.println("cpf: " + r.getCpf());
//				System.err.println("email: " + r.getEmail());
			}
//			for(int i = 0; i < response.length; i++){
//				System.err.println("string: " + response[i]);
//			}

		}catch(Exception e) { 
			System.err.println(e); 
		}
		method.releaseConnection();
		return r;
	}
	
	public static Retorno recuperarSenha(String email){
		HttpClient client = new HttpClient();
		String[] response;
		Retorno r = new Retorno();
      
		String params = "r=autenticacao/recuperarSenha&email=" + email;
		GetMethod method = new GetMethod("http://server.felipegasparini.com/autenticacao/index.php" + "?" + params);
      
		try {
			System.err.println("Method: " + method.getURI());
		} catch (URIException e1) {
			e1.printStackTrace();
		}
      
		try{
			client.executeMethod(method);
			System.err.println("response: " + method.getResponseBodyAsString());
			response = method.getResponseBodyAsString().split("\"");
			if(response[1].equals("erro")){
				r.setErro(response[3]);
				System.err.println("erro: " + r.getErro());
			}else{
				r.setSenha(response[3]);
				System.err.println("password: " + r.getSenha());
			}
//			for(int i = 0; i < response.length; i++){
//				System.err.println("string: " + response[i]);
//			}

		}catch(Exception e) { 
			System.err.println(e); 
		}
		method.releaseConnection();
		return r;
	}
}